package com.wuad.platformwuad.module.study.activiti7Demo.bank;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Comment;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 审批记录
 *
 */
public class ApprovalHistory {

    /**
     * 流程审批记录
     */
    @Test
    public void getApproveHistory1() {
        String taskId = "255006";
        List<ApprovalHistoryEntity> httpApprovalHistoryList = new ArrayList<ApprovalHistoryEntity>();
        List<Comment> list = getProcessComments(taskId);
        for (Comment comment : list) {
            ApprovalHistoryEntity httpApprovalHistory = new ApprovalHistoryEntity();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            httpApprovalHistory.setApprovalTime(dateFormat.format(comment.getTime()));
            httpApprovalHistory
                    .setApprovalUserName(comment.getUserId());
            //我前面添加建立的规则
            String[] str = comment.getFullMessage().split(":");
            String agree = str[0];
            String approvalOpinion = comment.getFullMessage().substring(2);
            httpApprovalHistory.setAgree(agree);
            httpApprovalHistory.setApprovalOpinion(approvalOpinion);
            httpApprovalHistoryList.add(httpApprovalHistory);
        }
        System.out.println(httpApprovalHistoryList);
    }

    /**
     * 获取历史审批记录comment
     *
     * @param taskId
     * @return
     */
    public List<Comment> getProcessComments(String taskId) {
        List<Comment> historyCommnets = new ArrayList<>();
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        // 1) 获取流程实例的ID
        HistoricTaskInstance task = processEngine.getHistoryService().createHistoricTaskInstanceQuery().taskId(taskId)
                .singleResult();
        String processInstanceId = task.getProcessInstanceId();
        // 2）通过流程实例查询所有的(用户任务类型)历史活动
        List<HistoricActivityInstance> hais = processEngine.getHistoryService().createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId).activityType("userTask").list();
        // 3）查询每个历史任务的批注
        for (HistoricActivityInstance hai : hais) {
            String historytaskId = hai.getTaskId();
            List<Comment> comments = processEngine.getTaskService().getTaskComments(historytaskId);
            // 4）如果当前任务有批注信息，添加到集合中
            if (comments != null && comments.size() > 0) {
                historyCommnets.addAll(comments);
            }
        }
        // 排序
        Collections.sort(historyCommnets, new Comparator<Comment>() {
            @Override
            public int compare(Comment o1, Comment o2) {
                return (int) -(o2.getTime().getTime() - o1.getTime().getTime());
            }
        });
        // 5）返回
        return historyCommnets;
    }

}
